package com.vcredit.creditcat.time;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import com.vcredit.creditcat.redis.RedisServiceExt;
import com.vcredit.framework.redis.RedisService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;
import org.springframework.stereotype.Component;

import com.vcredit.creditcat.manager.SpeedloanManager;
import com.vcredit.creditcat.model.ProcessSpecialCust;
import com.vcredit.creditcat.util.ConcurrentDateUtil;
import com.vcredit.creditcat.util.Utility;
import com.xxl.job.core.biz.model.ReturnT;
import com.xxl.job.core.handler.IJobHandler;
import com.xxl.job.core.handler.annotation.JobHander;


/**
 * 快速贷款的定时任务
 */
//@QuartzJob(name = "speedLoan2", cron = "0 0/11 * * * ?")
@JobHander(value="speedLoan2")
@Component
public class SpeedloanTime extends IJobHandler {

	private static Logger LOG = LoggerFactory.getLogger(SpeedloanTime.class);
	
	@Autowired
	private SpeedloanManager speedloanManager;
	
	@Autowired
	private ThreadPoolTaskExecutor threadPoolTaskExecutor;

	@Override
	public ReturnT<String> execute(String... strings) throws Exception {
		LOG.info("快速贷款认证定时任务执行了................" + ConcurrentDateUtil.formatDateTime(new Date()));

		/*
		 * 单线程  稳定后删除
		 * speedloanManager.handleSpeedloanTask();
		 */

		//多线程
		List<ProcessSpecialCust> handleList = new ArrayList<ProcessSpecialCust>();
		try {
			handleList = speedloanManager.find();
		} catch (Exception e) {
			LOG.error("快速贷款认证定时任务查询异常===",e);
			e.printStackTrace();
		}
		if (Utility.isNullOrEmpty(handleList)) {
			return ReturnT.SUCCESS;
		}
		LOG.info("当前任务线程池最大线程数========:"+threadPoolTaskExecutor.getMaxPoolSize());
		LOG.info("当前任务线程池当前线程数========:"+threadPoolTaskExecutor.getPoolSize());
		LOG.info("当前任务线程池活跃线程数========:"+threadPoolTaskExecutor.getActiveCount());
		LOG.info("快速贷款认证定时任务处理数量========:"+handleList.size());
		for (ProcessSpecialCust processSpecialCust : handleList) {
			threadPoolTaskExecutor.execute(new CrawleThread(processSpecialCust));
		}
		return ReturnT.SUCCESS;
	}

	class CrawleThread implements Runnable {
		private ProcessSpecialCust obj;
		private Logger logger = LoggerFactory.getLogger(CrawleThread.class);

		public CrawleThread(ProcessSpecialCust obj) {
			super();
			this.obj = obj;
		}

		public void run() {
			try {
				speedloanManager.handleSpeedloan(obj);
			} catch (Exception e) {
				logger.error("快速贷款认证定时任务处理异常===", e);
			}
		}
	}


}
